<template>
    <div class="achievement-detail-modal">
        <div class="modal-header">
            <van-icon name="cross" @click="handleClose" />
        </div>
        <div class="modal-content">
            <!-- 成就图标 -->
            <div class="achievement-icon-section">
                <div class="icon-container" :class="{ unlocked: achievement.is_unlocked }">
                    <van-image :src="achievement.icon_url || '/default-achievement.png'" width="80" height="80"
                        fit="cover" />
                    <div v-if="!achievement.is_unlocked" class="lock-overlay">
                        <van-icon name="lock" />
                    </div>
                    <div v-if="achievement.is_unlocked" class="unlock-glow"></div>
                </div>
                <div v-if="achievement.is_featured" class="featured-badge">
                    <van-icon name="fire" />
                    <span>Featured</span>
                </div>
            </div>

            <!-- 成就信息 -->
            <div class="achievement-info">
                <h2 class="achievement-title">{{ achievement.title }}</h2>
                <p class="achievement-description">{{ achievement.description }}</p>

                <!-- 奖励信息 -->
                <div class="rewards-section">
                    <div class="reward-item">
                        <van-icon name="diamond-o" />
                        <span>{{ achievement.points }} Points</span>
                    </div>
                    <div v-if="achievement.badge_url" class="reward-item">
                        <van-icon name="medal-o" />
                        <span>Special Badge</span>
                    </div>
                </div>

                <!-- 解锁状态 -->
                <div v-if="achievement.is_unlocked" class="unlock-status">
                    <div class="status-header">
                        <van-icon name="success" />
                        <span>Achievement Unlocked!</span>
                    </div>
                    <div class="unlock-date">
                        Unlocked on {{ formatDate(achievement.unlocked_at) }}
                    </div>
                </div>

                <!-- 进度信息 -->
                <div v-else-if="achievement.progress" class="progress-section">
                    <div class="progress-header">
                        <span>Progress</span>
                        <span class="progress-text">
                            {{ achievement.progress.current }}/{{ achievement.progress.target }}
                        </span>
                    </div>
                    <van-progress :percentage="(achievement.progress.current / achievement.progress.target) * 100"
                        stroke-width="8" color="#1989fa" track-color="#f5f5f5" />
                    <div class="progress-tips">
                        <p>{{ getProgressTip() }}</p>
                    </div>
                </div>

                <!-- 解锁条件 -->
                <div v-if="!achievement.is_unlocked" class="requirements-section">
                    <h3>How to unlock:</h3>
                    <div class="requirements-list">
                        <div v-for="(requirement, index) in achievement.requirements" :key="index"
                            class="requirement-item">
                            <van-icon name="info-o" />
                            <span>{{ requirement }}</span>
                        </div>
                    </div>
                </div>

                <!-- 相关成就 -->
                <div v-if="relatedAchievements.length > 0" class="related-section">
                    <h3>Related Achievements</h3>
                    <div class="related-list">
                        <div v-for="related in relatedAchievements" :key="related.id" class="related-item"
                            @click="viewRelated(related)">
                            <van-image :src="related.icon_url || '/default-achievement.png'" width="32" height="32"
                                fit="cover" />
                            <span>{{ related.title }}</span>
                            <van-icon name="arrow" />
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 底部操作 -->
        <div class="modal-footer">
            <van-button v-if="achievement.is_unlocked" type="primary" block @click="shareAchievement">
                <van-icon name="share-o" />
                Share Achievement
            </van-button>
            <van-button v-else type="primary" block @click="startTraining">
                Start Training
            </van-button>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { showToast } from 'vant'
import type { Achievement } from '@/api/achievement'

interface Props {
    achievement: Achievement
}

interface Emits {
    (e: 'close'): void
}

const props = defineProps<Props>()
const emit = defineEmits<Emits>()

// 模拟相关成就数据
const relatedAchievements = ref<Achievement[]>([
    {
        id: 2,
        title: 'Communication Master',
        icon_url: '/achievement-communication.png',
        is_unlocked: false
    } as Achievement,
    {
        id: 3,
        title: 'Team Player',
        icon_url: '/achievement-team.png',
        is_unlocked: true
    } as Achievement
])

const formatDate = (dateString?: string) => {
    if (!dateString) return ''
    const date = new Date(dateString)
    return date.toLocaleDateString('en-US', {
        year: 'numeric',
        month: 'long',
        day: 'numeric'
    })
}

const getProgressTip = () => {
    if (!props.achievement.progress) return ''

    const { current, target } = props.achievement.progress
    const remaining = target - current

    if (remaining <= 1) {
        return 'You\'re almost there! Just one more step to unlock this achievement.'
    } else if (remaining <= 3) {
        return `Only ${remaining} more to go! Keep up the great work.`
    } else {
        return `${remaining} more needed. Take it step by step, you can do it!`
    }
}

const handleClose = () => {
    emit('close')
}

const shareAchievement = async () => {
    try {
        if (navigator.share) {
            await navigator.share({
                title: `I unlocked "${props.achievement.title}"!`,
                text: `Check out my achievement in Social Dojo: ${props.achievement.description}`,
                url: window.location.href
            })
        } else {
            await navigator.clipboard.writeText(
                `I unlocked "${props.achievement.title}" in Social Dojo! ${window.location.href}`
            )
            showToast('Achievement shared to clipboard!')
        }
    } catch (error) {
        console.error('Failed to share achievement:', error)
        showToast('Failed to share achievement')
    }
}

const startTraining = () => {
    // 跳转到相关训练页面
    showToast('Redirecting to training...')
    handleClose()
}

const viewRelated = (achievement: Achievement) => {
    // 切换到相关成就
    showToast(`Viewing ${achievement.title}`)
}
</script>

<style lang="scss" scoped>
.achievement-detail-modal {
    width: 320px;
    max-height: 80vh;
    background: white;
    border-radius: 16px;
    overflow: hidden;
    display: flex;
    flex-direction: column;

    .modal-header {
        position: relative;
        padding: 16px;
        text-align: right;

        .van-icon {
            color: #969799;
            cursor: pointer;
            font-size: 18px;
        }
    }

    .modal-content {
        flex: 1;
        padding: 0 20px 20px;
        overflow-y: auto;

        .achievement-icon-section {
            position: relative;
            text-align: center;
            margin-bottom: 24px;

            .icon-container {
                position: relative;
                display: inline-block;
                margin-bottom: 12px;

                &.unlocked {
                    .van-image {
                        filter: none;
                    }
                }

                .van-image {
                    border-radius: 16px;
                    filter: grayscale(100%) brightness(0.7);
                    transition: filter 0.3s ease;
                }

                .lock-overlay {
                    position: absolute;
                    top: 0;
                    left: 0;
                    right: 0;
                    bottom: 0;
                    background: rgba(0, 0, 0, 0.6);
                    border-radius: 16px;
                    display: flex;
                    align-items: center;
                    justify-content: center;

                    .van-icon {
                        color: white;
                        font-size: 24px;
                    }
                }

                .unlock-glow {
                    position: absolute;
                    top: -4px;
                    left: -4px;
                    right: -4px;
                    bottom: -4px;
                    background: linear-gradient(45deg, #ffd21e, #ff6b35, #1989fa, #07c160);
                    border-radius: 20px;
                    z-index: -1;
                    animation: glow 2s ease-in-out infinite alternate;
                }
            }

            .featured-badge {
                display: inline-flex;
                align-items: center;
                gap: 4px;
                background: linear-gradient(135deg, #ee0a24, #ff6b35);
                color: white;
                padding: 4px 8px;
                border-radius: 12px;
                font-size: 12px;
                font-weight: 500;

                .van-icon {
                    font-size: 14px;
                }
            }
        }

        .achievement-info {
            .achievement-title {
                font-size: 20px;
                font-weight: 600;
                color: #323233;
                text-align: center;
                margin: 0 0 8px 0;
            }

            .achievement-description {
                font-size: 14px;
                color: #646566;
                text-align: center;
                line-height: 1.5;
                margin: 0 0 20px 0;
            }

            .rewards-section {
                display: flex;
                justify-content: center;
                gap: 20px;
                margin-bottom: 20px;

                .reward-item {
                    display: flex;
                    align-items: center;
                    gap: 6px;
                    font-size: 14px;
                    color: #646566;

                    .van-icon {
                        color: #1989fa;
                        font-size: 16px;
                    }
                }
            }

            .unlock-status {
                background: linear-gradient(135deg, #07c160, #00d4aa);
                color: white;
                padding: 16px;
                border-radius: 12px;
                text-align: center;
                margin-bottom: 20px;

                .status-header {
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    gap: 8px;
                    font-size: 16px;
                    font-weight: 600;
                    margin-bottom: 4px;

                    .van-icon {
                        font-size: 18px;
                    }
                }

                .unlock-date {
                    font-size: 13px;
                    opacity: 0.9;
                }
            }

            .progress-section {
                background: #f7f8fa;
                padding: 16px;
                border-radius: 12px;
                margin-bottom: 20px;

                .progress-header {
                    display: flex;
                    justify-content: space-between;
                    align-items: center;
                    margin-bottom: 12px;
                    font-size: 14px;
                    font-weight: 500;
                    color: #323233;

                    .progress-text {
                        color: #1989fa;
                    }
                }

                .progress-tips {
                    margin-top: 12px;

                    p {
                        font-size: 13px;
                        color: #646566;
                        line-height: 1.4;
                        margin: 0;
                    }
                }
            }

            .requirements-section {
                margin-bottom: 20px;

                h3 {
                    font-size: 16px;
                    font-weight: 600;
                    color: #323233;
                    margin: 0 0 12px 0;
                }

                .requirements-list {
                    .requirement-item {
                        display: flex;
                        align-items: flex-start;
                        gap: 8px;
                        margin-bottom: 8px;
                        font-size: 14px;
                        color: #646566;
                        line-height: 1.4;

                        .van-icon {
                            color: #1989fa;
                            font-size: 16px;
                            margin-top: 2px;
                            flex-shrink: 0;
                        }
                    }
                }
            }

            .related-section {
                h3 {
                    font-size: 16px;
                    font-weight: 600;
                    color: #323233;
                    margin: 0 0 12px 0;
                }

                .related-list {
                    .related-item {
                        display: flex;
                        align-items: center;
                        gap: 12px;
                        padding: 8px;
                        border-radius: 8px;
                        cursor: pointer;
                        transition: background-color 0.3s;

                        &:hover {
                            background: #f7f8fa;
                        }

                        span {
                            flex: 1;
                            font-size: 14px;
                            color: #323233;
                        }

                        .van-icon {
                            color: #969799;
                            font-size: 14px;
                        }
                    }
                }
            }
        }
    }

    .modal-footer {
        padding: 16px 20px;
        border-top: 1px solid #ebedf0;

        .van-button {
            height: 44px;
            font-size: 16px;
            font-weight: 500;

            .van-icon {
                margin-right: 6px;
            }
        }
    }
}

@keyframes glow {
    0% {
        opacity: 0.5;
        transform: scale(1);
    }

    100% {
        opacity: 0.8;
        transform: scale(1.05);
    }
}
</style>